import mcApi from 'mc-c2b2m-operator-api';
import { TreeSelect } from 'ant-design-vue';
import { BasicColumn, FormSchema } from '@/components/Table';

const { SpuModule } = mcApi;
const { BizMetaArrApi } = SpuModule;

const customRender = ({ text }) => {
  if (!text?.length) return;
  return text.map((item: any) => item.cnName).join('、');
};

export const tableColumns: BasicColumn[] = [
  { title: '名称', dataIndex: 'name' },
  { title: '条件', dataIndex: 'conditionList', customRender },
  { title: '展示结果', dataIndex: 'supportedResultList', customRender },
  { title: '隐藏结果', dataIndex: 'unsupportedResultList', customRender },
];

export const tableFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    component: 'Input',
    colProps: { span: 6 },
  },
];

const bizMetaAttrSchema: FormSchema = {
  field: 'conditionList',
  label: '条件',
  component: 'ApiTreeSelect',
  componentProps: {
    showSearch: true,
    valueField: 'id',
    labelInValue: true,
    labelField: 'cnName',
    treeCheckStrictly: true,
    api: BizMetaArrApi.getBizMetaAttrTree,
    treeDefaultExpandAll: true,
    childrenField: 'childBizMetaAttrList',
    showCheckedStrategy: TreeSelect.SHOW_ALL,
  },
};

export const modalFormSchema: FormSchema[] = [
  {
    ifShow: false,
    field: 'id',
    label: 'id',
    component: 'Input',
  },
  {
    required: true,
    field: 'name',
    label: '名称',
    component: 'Input',
  },
  {
    ...bizMetaAttrSchema,
    required: true,
  },
  {
    ...bizMetaAttrSchema,
    label: '展示结果',
    field: 'supportedResultList',
  },
  {
    ...bizMetaAttrSchema,
    label: '隐藏结果',
    field: 'unsupportedResultList',
  },
];
